//
//  GYStatusListViewModel.swift
//  Weibo
//
//  Created by vane on 16/8/13.
//  Copyright © 2016年 apple. All rights reserved.
//

import UIKit

class GYStatusListViewModel: NSObject {

     lazy var dataArray:[GYStatusViewModel] = [GYStatusViewModel]()

}
extension GYStatusListViewModel{
    // 请求首页数据 isPullUp 是否是上拉加载更多
    func loadData(isPullUp: Bool,finish:(isSuccess: Bool)->()){
        
        var sinceId: Int64 = 0
        var maxId:Int64 = 0
        
        if isPullUp{
            
            maxId = dataArray.last?.statusModel?.id ?? 0
            if maxId>0{
                // 则返回ID小于或等于max_id的微博，默认为0。
                maxId -= 1

            }
        
        }else{
            // 下拉刷新
            sinceId = dataArray.first?.statusModel?.id ?? 0

        }
        AFNetworkTools.sharedTools.homeLoadData(sinceId, maxId: maxId, success: { (response) -> () in
            guard let res = response?["statuses"] as? [[String:AnyObject]] else{
                finish(isSuccess: false)
                
                return
            }
            
            var tempArray:[GYStatusViewModel] = [GYStatusViewModel]()
            for dict in res {
                let status = GYStatusModel(dict: dict)
                let statusViewModel = GYStatusViewModel(statusModel: status)
                
                tempArray.append(statusViewModel)
            }
            
            //            self.dataArray = tempArray
            
            //赋值
            // 上拉
            if isPullUp {
                self.dataArray = self.dataArray + tempArray
            }else {
                // 下拉
                self.dataArray = tempArray + self.dataArray
            }
            
            
            finish(isSuccess: true)

            }) { (error) -> () in
                print("请求失败:\(error)")
                finish(isSuccess: false)
        }
    }
}